package com.xf.micro.core.service;

import com.xf.micro.core.base.MicroCore;
import com.xf.micro.core.filter.CoreFilter;
import com.xf.micro.core.filter.filters.ParamValidateFilter;

import java.util.*;

/**
 * @author zouxf
 * @date 2023/12/22 16:52
 * @description
 */
public class MicroCoreClient {

    static Map<Class, MicroCore> servicesMap = new HashMap<>();
    public static void main(String[] args) {
        loadServices();


    }

    static void loadServices() {
        ServiceLoader<MicroCore> load = ServiceLoader.load(MicroCore.class);
        Iterator<MicroCore> iterator = load.iterator();
        while (iterator.hasNext()) {
            MicroCore microCore = iterator.next();
            servicesMap.put(microCore.getClass(), microCore);
            System.out.println(microCore.getClass());
        }

        ServiceLoader<CoreFilter> coreFilters = ServiceLoader.load(CoreFilter.class);
        Iterator<CoreFilter> iterator1 = coreFilters.iterator();
        if (iterator1.hasNext()) {
            CoreFilter coreFilter = iterator1.next();
            System.out.println(coreFilter.getClass());
        }
    }

    static <T extends MicroCore> T findService(Class<T> c) {

        Set<Class> keySet = servicesMap.keySet();
        for (Class clazz : keySet) {
            if (c.isAssignableFrom(clazz)) {
                return (T) servicesMap.get(clazz);
            }
        }
        return null;
    }

}
